package com.example.myapplication.ui.home

import android.nfc.Tag
import android.os.Bundle
import android.os.Debug
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.R
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
//直接引用对应的xml 的Binding 即可
import com.example.myapplication.databinding.FragmentHomeBinding
import com.example.myapplication.databinding.ItemViewGridBinding
import com.example.myapplication.databinding.ItemViewLinerVerticalBinding

class HomeFragment : Fragment() {
    private var _binding:FragmentHomeBinding?=null
    private val binding get()=_binding!!  //表示不可能为空

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val homefragViewModel =
            ViewModelProvider(this).get(HomeViewModel::class.java)
        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val root: View = binding.root
        Log.d("debug","in home fragment onCreateView")//不能随便从网上吵 默认代码时最好的

//        val textView = binding.textHome  默认将ID驼峰方法 重命名
//        homefragViewModel.text.observe(viewLifecycleOwner) {
//            textView.text = "hahahaha"
//        }  可以直接修改原 HomeFragment上的textview上的文字
        //binding.recyclerView.layoutManager=LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false)
        binding.recyclerView.layoutManager=GridLayoutManager(context,2)
        binding.recyclerView.adapter=FruitAdapter()
        return root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    class FruitAdapter : RecyclerView.Adapter<FruitAdapter.ViewHolder>() {
        inner class ViewHolder(binding: ItemViewGridBinding) : RecyclerView.ViewHolder(binding.root) {
            val itemImage = binding.itemImage
            val itemtitle = binding.itemTitle
            val itemmsg = binding.itemMsg
        }
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            //重写 本类中的binding
            val binding = ItemViewGridBinding.inflate(LayoutInflater.from(parent.context), parent, false)
            return ViewHolder(binding)
        }
        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            holder.itemImage.setImageResource(R.drawable.remote)
            holder.itemtitle.text="[${position}]移动端架构师体系课"
            holder.itemmsg.text="移动端架构师体系课移动端架构师体系课移动端架构师体系课移动端架构师体系课移动端架构师体系课"
        }

        override fun getItemCount(): Int {
            return 20
        }

    }

}